﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using context;
using entities;

namespace DataAccessLibrary.DAL
{
    public class PromotionDAO
    {
        private BlueMDataContext _ctx = new BlueMDataContext();

        public List<vSubPromotion> SelectPromotion(int available)
        {
            DateTime date = DateTime.Now;
            try
            {
                IQueryable<vSubPromotion> queryable = _ctx.vSubPromotions;
                switch (available)
                {
                    case 1:
                        queryable = queryable.Where(p => p.PromotionEnd >= date);
                        break;
                    case 0:
                        queryable = queryable.Where(p => p.PromotionEnd < date);
                        break;
                }
                return queryable.ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public Promotion SelectPromotionById(int id)
        {
            return _ctx.Promotions.FirstOrDefault(p => p.PromotionID == id);
        }

        public List<vSubPromotion> SelectPromotionByTime(DateTime begin, DateTime end)
        {
            try
            {
                IQueryable<vSubPromotion> queryable =
                    _ctx.vSubPromotions.Where(p => p.PromotionStart >= begin && p.PromotionEnd <= end);
                return queryable.ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<vSubPromotion> SelectPromotionByTitle(string title)
        {
            try
            {
                IQueryable<vSubPromotion> queryable =
                    _ctx.vSubPromotions.Where(p => p.PromotionTitle.Contains(title));
                return queryable.ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public bool InsertPromotion(Promotion promotion)
        {
            try
            {
                _ctx.Promotions.InsertOnSubmit(promotion);
                _ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                _ctx = new BlueMDataContext();
                throw;
            }
        }

        public bool UpdatePromotion(Promotion promotion)
        {
            try
            {
                Promotion pro = SelectPromotionById(promotion.PromotionID);
                pro.PromotionEnd = promotion.PromotionEnd;
                pro.PromotionStart = promotion.PromotionStart;
                pro.PromotionDescription = promotion.PromotionDescription;
                pro.PromotionTitle = promotion.PromotionTitle;
                if (promotion.PromotionBanner != null)
                    pro.PromotionBanner = promotion.PromotionBanner;
                _ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                _ctx = new BlueMDataContext();
                throw;
            }
        }

        public bool DeletePromotion(int promotionId)
        {
            try
            {
                Promotion promotion = SelectPromotionById(promotionId);
                EntitySet<PromotionDetail> promotionDetails = promotion.PromotionDetails;
                _ctx.PromotionDetails.DeleteAllOnSubmit(promotionDetails);
                _ctx.Promotions.DeleteOnSubmit(promotion);
                _ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                _ctx = new BlueMDataContext();
                throw;
            }
        }

        public List<vSubPromotionDetail> SelectPromotionDetailByPromotionId(int promotionId)
        {
            return _ctx.vSubPromotionDetails.Where(p => p.PromotionID == promotionId).ToList();
        }

        public PromotionDetail SelectPromotionDetail(int promotionDetailId)
        {
            return _ctx.PromotionDetails.FirstOrDefault(p => p.PromotionDetailID == promotionDetailId);
        }


        public bool InsertPromotionDetail(int promotionId, string promotionContent, byte[] promotionImage)
        {
            try
            {
                var promotionDetail = new PromotionDetail
                                          {
                                              PromotionID = promotionId,
                                              PromotionContent = promotionContent,
                                              PromotionImage = promotionImage
                                          };
                _ctx.PromotionDetails.InsertOnSubmit(promotionDetail);
                _ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                _ctx = new BlueMDataContext();
                throw;
            }
        }

        public bool UpdatePromotionDetail(int promotionDetailId, string promotionContent, byte[] promotionImage)
        {
            try
            {
                PromotionDetail promotionDetail = SelectPromotionDetail(promotionDetailId);
                promotionDetail.PromotionContent = promotionContent;
                if (promotionImage != null)
                {
                    promotionDetail.PromotionImage = promotionImage;
                }
                _ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                _ctx = new BlueMDataContext();
                throw;
            }
        }

        public bool DeletePromotionDetail(int prmotionDetailId)
        {
            try
            {
                PromotionDetail promotionDetail = SelectPromotionDetail(prmotionDetailId);
                _ctx.PromotionDetails.DeleteOnSubmit(promotionDetail);
                _ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                _ctx = new BlueMDataContext();
                throw;
            }
        }
    }
}